package com.coocaa.x.service.litecontent.providers.store.host.com_coocaa_arsenal;

import android.content.Context;
import android.net.Uri;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.LiteContentProvider;
import com.coocaa.x.service.litecontent.data.ResultPackage;
import com.coocaa.x.service.litecontent.data.com_coocaa_arsenal.RP_Arsenal_Info;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_Boolean;
import com.skyworth.webSDK.webservice.tcappstore.AppStoreException;
import com.skyworth.webSDK.webservice.tcappstore.ArsenalBean;
import com.skyworth.webSDK.webservice.tcappstore.BulletBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xfk on 2015/11/10.
 */
public class StoreHost_Com_Coocaa__Arsenal extends LiteContentProvider.LiteContentProviderHost {
    private static final String MY_HOST = "com.coocaa.arsenal";
    private static StoreHost_Com_Coocaa__Arsenal instance = null;
    public StoreHost_Com_Coocaa__Arsenal(Context context, LiteContentProvider provider) {
        super(context, provider);
    }

    public static synchronized void create(Context context, LiteContentProvider provider) {
        if (instance == null)
            instance = new StoreHost_Com_Coocaa__Arsenal(context, provider);
    }

    private static RP_Arsenal_Info arsenal_info = null;
    private static RP_Boolean hasArsenal = null;
    @Override
    public String myHost() {
        return MY_HOST;
    }

    private LiteContentProvider.ResourceHandler hasArsenalHandler = new LiteContentProvider.ResourceHandler(this, "/openArsenal") {

        @Override
        public ResultPackage call(Uri uri) {
            LogUtils.i("1111", "1 hasArsenalHandler");
            if(hasArsenal == null || hasArsenal.resource == false) {
                hasArsenal = new RP_Boolean();
                try {
                    boolean b = CoocaaApplication.getCoocaaWebService().getAppStoreService().isOpenArsenal();
                    hasArsenal.resource = b;
                } catch (AppStoreException e) {
                    e.printStackTrace();
                }
            }

            LogUtils.i("1111", "hasArsenal.resource: "+hasArsenal.resource);
            return hasArsenal;
        }
    };

    private LiteContentProvider.ResourceHandler arsenalInfoHandler = new LiteContentProvider.ResourceHandler(this, "/arsenalInfo") {
        @Override
        public ResultPackage call(Uri uri) {
            LogUtils.i("1111", "arsenalInfoHandler");
            if(arsenal_info == null || arsenal_info.resource.resource_items == null
                    || arsenal_info.resource.resource_items.size() == 0) {
                LogUtils.i("11101", "arsenal_info == null");
                List<ArsenalBean> list = null;
                try {
                    list = CoocaaApplication.getCoocaaWebService().getAppStoreService().getArsenal();
                } catch (AppStoreException e) {
                    e.printStackTrace();
                }

                if(list != null && list.size() >0) {
                    arsenal_info = new RP_Arsenal_Info();

                    for (ArsenalBean arsenalBean: list) {
                        RP_Arsenal_Info.ArsenalClassData arsenalClassData = new RP_Arsenal_Info.ArsenalClassData();
                        arsenalClassData.name = arsenalBean.getType();
                        arsenalClassData.weaponList =  new ArrayList<RP_Arsenal_Info.WeaponItemData>();
                        if(arsenalBean.getArsenals() != null) {
                            for (BulletBean bulletBean: arsenalBean.getArsenals()) {
                                RP_Arsenal_Info.WeaponItemData weaponItemData = new RP_Arsenal_Info.WeaponItemData();
                                weaponItemData.price = bulletBean.getPrice();
                                weaponItemData.middleUrl = bulletBean.getViewPic();
                                weaponItemData.smallUrl = bulletBean.getIcon();
                                weaponItemData.bigUrl = bulletBean.getBigPic();
                                arsenalClassData.weaponList.add(weaponItemData);
                            }
                        }
                        arsenal_info.resource.add(arsenalClassData);
                    }
                }
            } else {
                LogUtils.i("11101", "arsenal_info != null");
            }
            return arsenal_info;
        }
    };
}
